package cn.jobstart.aop;

import cn.jobstart.common.HttpHelper;
import cn.jobstart.common.HttpTookit;
import cn.jobstart.common.StringUtil;
import cn.jobstart.modelExt.SysOperLogExt;
import cn.jobstart.pubInter.RestfulController;
import cn.jobstart.pubInter.aop.SystemLog;
import cn.jobstart.service.ISysOperLogService;
import cn.jobstart.web.common.token.AdminToken;
import cn.jobstart.web.common.token.JwtsUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author sunke
 * @Date 2020/12/20 18:47
 * @description
 */
@Aspect
@Configuration
@Slf4j
public class SystemLogAspect {

    @Resource(name = "jwtsUtil")
    private JwtsUtil jwtsUtil;

    @Resource(name = "sysOperLogService")
    private ISysOperLogService sysOperLogService;


    @Pointcut("@annotation(systemLog)")
    private void annotationPointCut(SystemLog systemLog) {
    }


    /**
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("annotationPointCut(systemLog)")
    public Object around(ProceedingJoinPoint joinPoint, SystemLog systemLog) throws Throwable {


        HttpServletRequest request = null;
        String modelName = "";
        if (joinPoint.getTarget() instanceof RestfulController) {
            modelName = ((RestfulController) joinPoint.getTarget()).getModelName();

        }
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            request = attributes.getRequest();
            Object result = joinPoint.proceed();


            saveDB(systemLog, 0, JSONObject.toJSONString(result), modelName, request);
            return result;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            saveDB(systemLog, 1, e.getMessage(), modelName, request);

            throw e;


        }


    }


    private void saveDB(SystemLog systemLog, int status, String msg, String modelName, HttpServletRequest request) {

        try {
            SysOperLogExt sysOperLog = new SysOperLogExt();
            sysOperLog.setBusinessType(systemLog.businessType());
            if (StringUtil.isNotEmpty(modelName)) {
                sysOperLog.setTitle(modelName + "-" + systemLog.title());
            } else {
                sysOperLog.setTitle(systemLog.title());
            }
            sysOperLog.setMethod(systemLog.method());
            sysOperLog.setOperIp(HttpTookit.getRealIpAddr(request));
            sysOperLog.setOperUrl(request.getRequestURI());

            String body = HttpHelper.getBodyString(request);
            if (body != null) {
                body = body.replaceAll("\r|\n|\t", "");
            }
            sysOperLog.setOperParam(body);
            sysOperLog.setStatus(status);
            sysOperLog.setOperatorType(systemLog.operatorType());
            sysOperLog.setErrorMsg(msg);
            sysOperLog.setOperTime(new Date());

            AdminToken adminToken = jwtsUtil.getAdminToken(request);
            if (adminToken != null) {
                sysOperLog.setOperName(adminToken.getTruename());

            }

            sysOperLogService.insert(sysOperLog);
        } catch (Exception e) {

        }


    }


}




